home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / obj2asm.zip / OEXTRA.C < prev    next >
Text File  |  1991-10-02  |  10KB  |  365 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "o.h"
  5.  
  6. #define LINE_SIZE   80
  7.  
  8. /*
  9. ** Local Prototypes
  10. */
  11. SEG_T *find_seg_by_name( char * );
  12. char *ignore_whitespace( char * );
  13. char *get_text( char *, char * );
  14. char *get_num( char *, dword * );
  15.  
  16. struct hint_word_s {
  17.     char    *text;
  18.     int     type;
  19. };
  20.  
  21. struct hint_word_s hint_words[] = {
  22.     { "DB", BYTE_PTR },
  23.     { "DW", WORD_PTR },
  24.     { "DD", DWORD_PTR },
  25.     { "DF", FWORD_PTR },
  26.     { "DQ", QWORD_PTR },
  27.     { "DT", TBYTE_PTR },
  28. };
  29.  
  30. int hint_nwords = sizeof(hint_words)/sizeof(char *);
  31.  
  32. int hint_compare( rec_1, rec_2 )
  33.     HINT_T  *rec_1;
  34.     HINT_T  *rec_2;
  35. {
  36.     if ( rec_1->seg_idx > rec_2->seg_idx ) {
  37.         return( LEFT );
  38.     } else {
  39.         if ( rec_1->seg_idx < rec_2->seg_idx ) {
  40.             return( RIGHT );
  41.         } else {
  42.             if ( rec_1->offset > rec_2->offset ) {
  43.                 return( LEFT );
  44.             } else {
  45.                 if ( rec_1->offset < rec_2->offset ) {
  46.                     return( RIGHT );
  47.                 } else {
  48.                     return( EQUAL );
  49.                 }
  50.             }
  51.         }
  52.     }
  53. }
  54.  
  55.  
  56.  
  57. void hint_insert( int seg_idx, dword offset, int hint_type, dword length )
  58. {
  59.     HINT_T          *hint_rec;
  60.  
  61.     hint_rec = (HINT_T *)o_malloc( sizeof(HINT_T) );
  62.     hint_rec->seg_idx   = seg_idx;
  63.     hint_rec->offset    = offset;
  64.     hint_rec->hint_type = hint_type;
  65.     hint_rec->length    = length;
  66.     insert( (char *)hint_rec, hint_tree, TC hint_compare );
  67. }
  68.  
  69. SEG_T *find_seg_by_name( seg_text )
  70.     char    *seg_text;
  71. {
  72.     SEG_T   *seg_rec;
  73.     NODE_T  *seg_node;
  74.     NAME_T  search;
  75.     NAME_T  *name;
  76.  
  77.     seg_node = start( segment_tree, RIGHT );
  78.     while ( seg_node != segment_tree ) {
  79.         seg_rec = (SEG_T *)seg_node->data;
  80.         search.index = seg_rec->name;
  81.         name = (NAME_T *)find( (char *)&search, name_tree,
  82.                                             TC name_compare, NULL );
  83.         if ( name == NULL ) fmt_error( "Undefined segment name" );
  84.  
  85.         if ( strcmp(name->name,seg_text) == 0 ) {
  86.             return( seg_rec );
  87.         }
  88.         seg_node = traverse( seg_node, RIGHT );
  89.     }
  90.     return( NULL );
  91. }
  92.  
  93. char *ignore_whitespace( start )
  94.     char    *start;
  95. {
  96.     char    *pc;
  97.     char    ch;
  98.  
  99.     pc = start;
  100.     while ( (ch=*pc) != '\0' ) {
  101.         if ( ch != ' ' && ch != '\t' && ch != '\n' ) {
  102.             break;
  103.         }
  104.         pc++;
  105.     }
  106.     return( pc );
  107. }
  108.  
  109. char *get_text( destination, start )
  110.     char    *destination;
  111.     char    *start;
  112. {
  113.     char    *pc;
  114.     char    ch;
  115.     int     len;
  116.  
  117.     len = 0;
  118.     pc = start;
  119.     while ( (ch=*pc) != ' ' && ch != '\t' && ch != '=' && ch != '\0'
  120.             && ch != ':' && ch != '\n' ) {
  121.         *destination++ = ch;
  122.         pc++;
  123.         len++;
  124.         if ( len == NAMESIZE ) {
  125.             break;
  126.         }
  127.     }
  128.     *destination = '\0';
  129.     return( pc );
  130. }
  131.  
  132. char *get_num( start, value )
  133.     char            *start;
  134.     dword   *value;
  135. {
  136.     char            *pc;
  137.     char            ch;
  138.     dword   dec_form;
  139.     dword   hex_form;
  140.     int             is_hex;
  141.     int             decable;
  142.     int             char_ok;
  143.  
  144.     dec_form = 0L;
  145.     hex_form = 0L;
  146.  
  147.     pc = start;
  148.     is_hex = FALSE;
  149.     decable = TRUE;
  150.  
  151.     while ( (ch=*pc) != '\0' ) {
  152.         char_ok = FALSE;
  153.         if ( ch >= '0' && ch <= '9' ) {
  154.             dec_form *= 10;
  155.             dec_form += ch - '0';
  156.             hex_form *= 16;
  157.             hex_form += ch - '0';
  158.             char_ok = TRUE;
  159.         }
  160.         if ( ch >= 'a' && ch <= 'f' ) {
  161.             decable = FALSE;
  162.             hex_form *= 10;
  163.             hex_form += ch - 'a' + 10;
  164.             char_ok = TRUE;
  165.         }
  166.         if ( ch >= 'A' && ch <= 'F' ) {
  167.             decable = FALSE;
  168.             hex_form *= 10;
  169.             hex_form += ch - 'A' + 10;
  170.             char_ok = TRUE;
  171.         }
  172.         if ( ch == 'h' || ch == 'H' ) {
  173.             is_hex = TRUE;
  174.             pc++;
  175.             break;
  176.         }
  177.         if ( !char_ok ) {
  178.             break;
  179.         }
  180.         pc++;
  181.     }
  182.     if ( is_hex ) {
  183.         *value = hex_form;
  184.     } else {
  185.         if ( decable ) {
  186.             *value = dec_form;
  187.         } else {
  188.             *value = 0L;
  189.         }
  190.     }
  191.     return( pc );
  192. }
  193.  
  194. void load_extra( exename, filename )
  195.     char            *exename;
  196.     char            *filename;
  197. {
  198.     FILE            *e_file;
  199.     char            temp_name[50];
  200.     char            e_line[LINE_SIZE+1];
  201.     int             line_num;
  202.     char            *pc;
  203.     char            *semicolon;
  204.     char            *equal;
  205.     char            *colon;
  206.     char            seg_text[NAMESIZE+1];
  207.     char            lab_text[NAMESIZE+1];
  208.     int             segment_type;
  209.     dword   disp;
  210.     int             count;
  211.  
  212. #ifdef DEBUG
  213.     printf("Loading from extra file %s\n", filename );
  214. #endif
  215.  
  216.     strcpy( temp_name, filename );
  217.     if ( strchr(temp_name,'.') == NULL ) {  /* Append ".add" if not extension */
  218.         strcat( temp_name, ".add" );        /* is supplied                    */
  219.     }
  220.     e_file = fopen( temp_name, "r" );
  221.     if ( e_file == NULL ) {
  222.         fprintf( stderr, "%s: Cannot open %s\n", exename, temp_name );
  223.         exit(6);
  224.     }
  225.  
  226.     line_num = 1;
  227.  
  228.     /*
  229.     ** Process lines of format:
  230.     **
  231.     **  SEG segname CODE/DATA                 Pick segment type
  232.     **  labname = segname : offset            Create label
  233.     **  segname : offset : DB/DW/DD/DF/DQ/DT  Control dis-assembly
  234.     */
  235.  
  236.     while ( fgets(e_line,LINE_SIZE,e_file) != NULL ) {
  237.         semicolon = strchr( e_line, ';' );
  238.         if ( semicolon ) {
  239.             *semicolon = '\0';
  240.         }
  241.         pc = ignore_whitespace( e_line );
  242. #ifdef DEBUG
  243. fprintf(stderr, "%s", pc );
  244. #endif
  245.         if ( strnicmp(pc,"SEG ",4) == 0 ) {
  246.             pc = ignore_whitespace( pc+4 );
  247.             pc = get_text( seg_text, pc );
  248.             pc = ignore_whitespace( pc );
  249.             segment_type = 0;
  250.             if ( strnicmp(pc,"CODE",4) == 0 ) {
  251.                 pc += 4;
  252.                 segment_type = 1;
  253.             }
  254.             if ( strnicmp(pc,"DATA",4) == 0 ) {
  255.                 pc += 4;
  256.                 segment_type = 2;
  257.             }
  258.             if ( segment_type == 0 ) {
  259.                 fprintf( stderr,
  260.                   "%s: Syntax error on line %d of %s (should be CODE/DATA).\n",
  261.                   exename, line_num, temp_name );
  262.                 exit(7);
  263.             }
  264.             seg_rec = find_seg_by_name( seg_text );
  265.             if ( !seg_rec ) {
  266.                 fprintf( stderr,
  267.             "%s: Syntax error on line %d of %s (segment '%s' not found)\n",
  268.                     exename, line_num, temp_name, seg_text );
  269.                 exit(7);
  270.             }
  271.             if ( segment_type == 1 ) {
  272.                 seg_rec->code = TRUE;
  273.             } else {
  274.                 seg_rec->code = FALSE;
  275.             }
  276.         }
  277.         equal = strchr(pc, '=' );
  278.         if ( equal ) {
  279.             pc = get_text(lab_text, pc );
  280.             pc = ignore_whitespace(pc);
  281.             if ( strnicmp(pc,"=",1) != 0 ) {
  282.                 fprintf( stderr,
  283.             "%s: Syntax error on line %d of %s (expecting '=' after label)\n",
  284.                     exename, line_num, temp_name );
  285.                 exit(7);
  286.             }
  287.             pc=ignore_whitespace(pc+1);
  288.             colon=strchr(pc,':');
  289.             if ( colon == NULL ) {
  290.                 fprintf( stderr,
  291.             "%s: Syntax error on line %d of %s (expecting ':')\n",
  292.                     exename, line_num, temp_name );
  293.                 exit(7);
  294.             }
  295.             pc = get_text( seg_text, pc );
  296.             pc = ignore_whitespace( colon+1 );
  297.             pc = get_num( pc, &disp );
  298.             seg_rec = find_seg_by_name( seg_text );
  299.             if ( !seg_rec ) {
  300.                 fprintf( stderr,
  301.             "%s: Syntax error on line %d of %s (segment '%s' not found)\n",
  302.                     exename, line_num, temp_name, seg_text );
  303.                 exit(7);
  304.             }
  305.             pub_insert( seg_rec->index, disp, lab_text, LOCAL, FALSE );
  306.         }
  307.         colon = strchr(pc,':');
  308.         if ( colon ) {
  309.             pc = get_text( seg_text, pc );
  310.             seg_rec = find_seg_by_name( seg_text );
  311.             if ( !seg_rec ) {
  312.                 fprintf( stderr,
  313.             "%s: Syntax error on line %d of %s (segment '%s' not found)\n",
  314.                     exename, line_num, temp_name, seg_text );
  315.                 exit(7);
  316.             }
  317.             pc = ignore_whitespace( pc );
  318.             if ( strnicmp(pc,":",1) != 0 ) {
  319.                 fprintf( stderr,
  320.             "%s: Syntax error on line %d of %s (expecting ':' seperator)\n",
  321.                     exename, line_num, temp_name );
  322.                 exit(7);
  323.             }
  324.             pc = get_num( pc+1, &disp );
  325.             pc = ignore_whitespace( pc );
  326.             if ( strnicmp(pc,":",1) != 0 ) {
  327.                 fprintf( stderr,
  328.             "%s: Syntax error on line %d of %s (expecting ':' seperator)\n",
  329.                     exename, line_num, temp_name );
  330.                 exit(7);
  331.             }
  332.             pc = ignore_whitespace( pc+1 );
  333.             pc = get_text( lab_text, pc );  /* Is really data type text */
  334.             count = 0;
  335.             while ( count < hint_nwords ) {
  336.                 if ( stricmp(lab_text,hint_words[count].text) == 0 ) {
  337.                     break;
  338.                 }
  339.                 count++;
  340.             }
  341.             if ( count == hint_nwords ) {
  342.                 fprintf( stderr,
  343.             "%s: Syntax error on line %d of %s (expecting DB/DW/DD/DF/DQ/DT)\n",
  344.                     exename, line_num, temp_name );
  345.                 exit(7);
  346.             }
  347.             hint_insert(seg_rec->index,disp,hint_words[count].type,1);
  348.         }
  349.         pc = ignore_whitespace( pc );
  350.         if ( strlen(pc) != 0 ) {
  351.                 fprintf( stderr,
  352.        "%s: Syntax error on line %d of %s (too many characters on line '%s')\n",
  353.                     exename, line_num, temp_name, pc );
  354.                 exit(7);
  355.         }
  356.         line_num++;
  357.     }
  358.  
  359. #ifdef DEBUG
  360. fprintf(stderr, "\n", e_line );
  361. #endif
  362.  
  363.     fclose( e_file );
  364. }
  365.